package org.alex.samples.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.alex.samples.dao.IExamSubjectDAO;
import org.alex.samples.dao.IUserJdbcDAO;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamUserRoleVO;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.model.UserJDBC;
import org.alex.samples.util.ExcelUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class UserJdbcServiceImpl implements IUserJdbcService {
	
	@Autowired
	private IUserJdbcDAO userJdbcDAO;
	
	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	//查询全部
	@Override
	public List<UserJDBC> findAllUserJdbcs(){
		return userJdbcDAO.findAllUsers();
	}
	
	//新增学生
	@Override
	public void createUserJdbc(UserJDBC userJDBC) throws Exception{
		userJdbcDAO.createUser(userJDBC);
	}
	
	//删除学生
	@Override
	public void deleteUserJdbc(String userId) throws Exception {
		userJdbcDAO.deleteUser(userId);
	}
	
	//修改学生
	@Override
	public void editUserJdbc(UserJDBC userJDBC) throws Exception {
		userJdbcDAO.editUser(userJDBC);		
	}
	
	//查询某个学生
	@Override
	public UserJDBC getUserJdbcById(String userId) {
		return userJdbcDAO.getUserById(userId);
	}

	//查询某个学生
	@Override
	public ExamUserVO getUserJdbcById1(String userId) {
		return userJdbcDAO.getUserById1(userId);
	}
	
	//查询中间表对象 用户-角色
	@Override
	public ExamUserRoleVO getExamUserRole(String userId) {
		return userJdbcDAO.getExamUserRole(userId);
	}
	
	//修改用户登录错误的次数
	@Override
	public void updateUserLoginFailCount(ExamUserVO resultVO) {
		UserJDBC user=new UserJDBC();
		BeanUtils.copyProperties(resultVO, user);
		userJdbcDAO.updateUserLoginFailCount(user);
		
	}
	
	//修改用户锁定时间
	@Override
	public void updateUserLastLockTime(ExamUserVO resultVO) {
		UserJDBC user=new UserJDBC();
		BeanUtils.copyProperties(resultVO, user);
		userJdbcDAO.updateUserLastLockTime(user);
		
	}

	
	//根据Excel内容，批量增加学生
	@Override
	public void saveStudentByExcel(InputStream inputStream, String postfix) {
		Workbook wk=ExcelUtils.openWorkbook(inputStream,postfix);
		List<ExamUserVO> allExcelUser=ExcelUtils.getStudentListByExcel(wk);
		List<UserJDBC> entities=new ArrayList<UserJDBC>();
		if(allExcelUser!=null){
			UserJDBC entity=null;
			for(int i=0;i<allExcelUser.size();i++){
				entity=new UserJDBC();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				entities.add(entity);
			}
		}
		userJdbcDAO.saveOrUpdate(entities);
	}

	//查询全部科目
	@Override
	public List<ExamSubjectVO> getAllSubject() {
		List<ExamSubject> subjectList=examSubjectDAO.findAllExamSubject();
		List<ExamSubjectVO> resultList=new ArrayList<ExamSubjectVO>();
		for(ExamSubject examSubject:subjectList){
			ExamSubjectVO vo=new ExamSubjectVO();
			BeanUtils.copyProperties(examSubject, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	//查询该学生关联的科目
	@Override
	public List<String> getAllSubjectByUser(String userId) {
		return examSubjectDAO.getAllSubjectByUser(userId);
	}

	
	//保存学生科目
	@Override
	public void saveStudentSubject(List<String> studentSubjects, String userId) {
		List<String> dbSubjectUserList=examSubjectDAO.getAllSubjectByUser(userId);//获取当前学生关联的所有科目
		List<String> insertList=new ArrayList<String>();
		List<String> delList=new ArrayList<String>();
		//1有，2无 的情况
		//判断哪些学生是最新勾选上的
		for(String selectedSubject:studentSubjects){
			if(!dbSubjectUserList.contains(selectedSubject)){
				insertList.add(selectedSubject);
			}
		}
		//1无，2有的情况
		//判断哪些学生本来是勾选上的，现在去掉勾选的
		for(String selectedSubject:dbSubjectUserList){
			if(!studentSubjects.contains(selectedSubject)){
				delList.add(selectedSubject);
			}
		}
		//insertList 分别保存
		examSubjectDAO.insertUserSubjects(insertList, userId);
		//delList 分别删除
		examSubjectDAO.deleteUserSubjects(delList, userId);
	}



	
}
